package com.roger.myHome.service.impl;

import com.alibaba.excel.EasyExcel;
import com.roger.myHome.domain.TbBloodSugar;
import com.roger.myHome.entity.dto.BloodSugarAddReq;
import com.roger.myHome.entity.qto.BloodSugarReq;
import com.roger.myHome.entity.vo.BloodSugarVo;
import com.roger.myHome.entity.export.BloodSugarEntity;
import com.roger.myHome.exception.DuplicateException;
import com.roger.myHome.mapper.BloodSugarsMapper;
import com.roger.myHome.page.EUDataGridResult;
import com.roger.myHome.service.BloodSugarService;
import com.roger.myHome.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Service
public class BloodSugarServiceImpl implements BloodSugarService {



    @Resource
    private BloodSugarsMapper bloodSugarsMapper;

    @Override
    public EUDataGridResult page(BloodSugarReq bloodSugarReq) {
        List<BloodSugarVo> list = bloodSugarsMapper.selectBloodSugarPage(bloodSugarReq);

        return new EUDataGridResult(list,bloodSugarsMapper.selectBloodSugarCount(bloodSugarReq));
    }


    @Override
    public String add(BloodSugarAddReq req) {
        Map<String,String> hashMap = new HashMap();
        //先判断记录是否存在
        try {
            String mt = req.getMeasureTime().substring(0,10);
            List<Long> list = bloodSugarsMapper.validate(mt,req.getTimePeriod());

            if (!list.isEmpty()) {
                hashMap.put("result", "该记录已存在");
            } else {
                TbBloodSugar bloodSugar = new TbBloodSugar();
                BeanUtils.copyProperties(req,bloodSugar);

                bloodSugarsMapper.insert(bloodSugar);
                hashMap.put("result", "新增成功!");
            }

        } catch (Exception e) {
            e.printStackTrace();
            hashMap.put("result", "新增失败!");
        }

        return StringUtils.getJSONString(hashMap);
    }

    @Override
    public String add2(BloodSugarAddReq req) {
        Map<String,String> hashMap = new HashMap();
        //先判断记录是否存在
        String mt = req.getMeasureTime().substring(0,10);
        List<Long> list = bloodSugarsMapper.validate(mt,req.getTimePeriod());

        if (!list.isEmpty()) {
            throw new DuplicateException("该记录已存在");
        } else {
            TbBloodSugar bloodSugar = new TbBloodSugar();
            BeanUtils.copyProperties(req,bloodSugar);

            bloodSugarsMapper.insert(bloodSugar);
            hashMap.put("result", "新增成功!");
        }

        return StringUtils.getJSONString(hashMap);
    }


    @Override
    public void export(HttpServletResponse response) {
        List<BloodSugarEntity> list = bloodSugarsMapper.selectBloodSugarExport();
        Collections.reverse(list);
        try {
            //HttpServletResponse消息头参数设置
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Transfer-Encoding", "binary");
            response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Pragma", "public");
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
            String fileName = "导出列表" + ".xlsx";
            fileName = new String(fileName.getBytes(), "ISO-8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName );
            EasyExcel.write(response.getOutputStream(), BloodSugarEntity.class)
                    .autoCloseStream(Boolean.FALSE)
                    .sheet("导出列表")
                    .doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String delete(Long id) {
        bloodSugarsMapper.deleteById(id);
        Map hashMap = new HashMap();
        hashMap.put("result", "删除成功!");
        return StringUtils.getJSONString(hashMap);
    }

    public static void main(String[] args) {
        String e = "\"jingziming@beyondcent.com\"";
        List<String> list = new ArrayList<>();
        list.add(e);
        String re = org.apache.commons.lang.StringUtils.join(list.toArray(),",");
        System.out.println(re);
        List<String> list2 = Arrays.asList(re.split(","));
        System.out.println(list2);
    }
}
